1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.google.common.collect;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18
19 import com.google.common.annotations.GwtCompatible;
20
21 import java.util.Comparator;
22 import java.util.SortedSet;
23
24
25
26
27
28
29 @GwtCompatible
30 final class SortedIterables {
31 private SortedIterables() {}
32
33
34
35
36
37 public static boolean hasSameComparator(Comparator<?> comparator, Iterable<?> elements) {
38 checkNotNull(comparator);
39 checkNotNull(elements);
40 Comparator<?> comparator2;
41 if (elements instanceof SortedSet) {
42 comparator2 = comparator((SortedSet<?>) elements);
43 } else if (elements instanceof SortedIterable) {
44 comparator2 = ((SortedIterable<?>) elements).comparator();
45 } else {
46 return false;
47 }
48 return comparator.equals(comparator2);
49 }
50
51 @SuppressWarnings("unchecked")
52
53 public static <E> Comparator<? super E> comparator(SortedSet<E> sortedSet) {
54 Comparator<? super E> result = sortedSet.comparator();
55 if (result == null) {
56 result = (Comparator<? super E>) Ordering.natural();
57 }
58 return result;
59 }
60 }